Micron Document
`:top
An `!undefined variable`! in the `F33f`_`[source code`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Source_code]`_`f of a `F33f`_`[computer program`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Computer_program]`_`f is a `F33f`_`[variable`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Variable_(programming)]`_`f that is accessed in the code but has not been `F33f`_`[declared`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Declaration_(computer_science)]`_`f by that code.`:cite-ref-1[`F5bf`_`[1`#cite-note-1]`_`f]

In some `F33f`_`[programming languages`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Programming_languages]`_`f, an implicit declaration is provided the first time such a variable is encountered at `F33f`_`[compile time`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Compile_time]`_`f. In other languages such a usage is considered to be sufficiently serious that a diagnostic being issued and the compilation fails.

Some language definitions initially used the implicit declaration behavior and as they matured provided an option to disable it (e.g. `F33f`_`[Perl`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Perl]`_`f's "`B100`F9d9use warnings`f`b" or `F33f`_`[Visual Basic`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Visual_Basic]`_`f's "`B100`F9d9Option Explicit`f`b").

>>Contents

• `F0af`_`[Examples`#examples]`_`f
• `F0af`_`[CLISP`#clisp]`_`f
• `F0af`_`[C`#c]`_`f
• `F0af`_`[JavaScript`#javascript]`_`f
• `F0af`_`[Lua`#lua]`_`f
• `F0af`_`[ML (Standard ML of New Jersey)`#ml-standard-ml-of-new-jersey]`_`f
• `F0af`_`[MUMPS`#mumps]`_`f
• `F0af`_`[OCaml`#ocaml]`_`f
• `F0af`_`[Perl`#perl]`_`f
• `F0af`_`[PHP 5`#php-5]`_`f
• `F0af`_`[Python`#python]`_`f
• `F0af`_`[REXX`#rexx]`_`f
• `F0af`_`[Ruby`#ruby]`_`f
• `F0af`_`[Tcl`#tcl]`_`f
• `F0af`_`[VBScript`#vbscript]`_`f
• `F0af`_`[References`#references]`_`f

-─

>>Examples

The following provides some examples of how various programming language implementations respond to undefined variables. Each code snippet is followed by an error message (if any).

>>>CLISP

`B100`F9d9(setf y x)`f`b

`B100`F9d9*** - EVAL: variable X has no value`f`b

>>>C

`B100`F9d9int main() {`f`b
`B100`F9d9 int y = x;`f`b
`B100`F9d9 return 0;`f`b
`B100`F9d9}`f`b

`B100`F9d9foo.c: In function \`main':`f`b
`B100`F9d9foo.c:2: error: \`x' undeclared (first use in this function)`f`b
`B100`F9d9foo.c:2: error: (Each undeclared identifier is reported only once`f`b
`B100`F9d9foo.c:2: error: for each function it appears in.)`f`b

>>>JavaScript

A ReferenceError only happens if the same piece of executed code has a `B100`F9d9let`f`b or a `B100`F9d9const`f`b (but not `B100`F9d9var`f`b) declaration later on, or if the code is executed in strict mode. In all other cases, the variable will have the special value `B100`F9d9undefined`f`b.

`B100`F9d9"use strict";`f`b
`B100`F9d9let y = x;`f`b

`B100`F9d9let y = x;`f`b
`B100`F9d9let x; // causes error on line 1`f`b

`B100`F9d9 ReferenceError: x is not defined`f`b
`B100`F9d9 Source File: file:///c:/temp/foo.js`f`b

>>>Lua

`B100`F9d9y = x`f`b

(no error, continuing)

`B100`F9d9print(y)`f`b

`B100`F9d9nil`f`b

>>>ML (Standard ML of New Jersey)

`B100`F9d9val y = x;`f`b

`B100`F9d9stdIn:1.9 Error: unbound variable or constructor: x`f`b

>>>MUMPS

`B100`F9d9Set Y=X`f`b

`B100`F9d9<UNDEF>`f`b

>>>OCaml

`B100`F9d9let y = x;;`f`b

`B100`F9d9Unbound value x`f`b

>>>Perl

`B100`F9d9my $y = ($x // 0) + 1; # defined-or operator`f`b

`B100`F9d9(no error)`f`b

>>>PHP 5

`B100`F9d9$y = $x;`f`b

`B100`F9d9(no error)`f`b

`B100`F9d9$y="";`f`b
`B100`F9d9$x="";`f`b
`B100`F9d9error_reporting(E_ALL);`f`b
`B100`F9d9$y = $x;`f`b

`B100`F9d9PHP Notice: Undefined variable: x in foo.php on line 3`f`b

>>>Python

>>>>Python 3

`B100`F9d9x = y`f`b
`B100`F9d9Traceback (most recent call last):`f`b
`B100`F9d9 File "<pyshell#0>", line 1, in <module>`f`b
`B100`F9d9 x = y`f`b
`B100`F9d9NameError: name 'y' is not defined`f`b

>>>>Python 2.4

`B100`F9d9>>> x = y`f`b
`B100`F9d9Traceback (most recent call last):`f`b
`B100`F9d9 File "<stdin>", line 1, in <module>`f`b
`B100`F9d9NameError: name 'y' is not defined`f`b

>>>REXX

`B100`F9d9signal on novalue`f`b
`B100`F9d9y = x`f`b

`B100`F9d9+++ Error 30 in line 2: Label not found`f`b

>>>Ruby

`B100`F9d9irb(main):001:0> y = x`f`b
`B100`F9d9NameError: undefined local variable or method \`x' for main:Object`f`b
`B100`F9d9 from (irb):1`f`b

>>>Tcl

`B100`F9d9% set y $x`f`b
`B100`F9d9can't read "x": no such variable`f`b

>>>VBScript

`B100`F9d9Dim y`f`b
`B100`F9d9y = x`f`b

`B100`F9d9(no error)`f`b

`B100`F9d9Option Explicit`f`b
`B100`F9d9`f`b
`B100`F9d9Dim y`f`b
`B100`F9d9y = x`f`b

`B100`F9d9(3, 1) Microsoft VBScript runtime error: Variable is undefined: 'x'`f`b

>>References

`:cite-note-1`!1.`! `F0af`_`[↑`#cite-ref-1]`_`f "undefined variable." YourDictionary, n.d. Web. 24 July 2013. <http://computer.yourdictionary.com/undefined-variable Archived 2013-04-15 at the `F33f`_`[Wayback Machine`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Wayback_Machine]`_`f>.

`c`F0af`_`[↑ Back to top`#top]`_`f`a